package com.lizonghui.googleplay.control;

import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.lizonghui.googleplay.R;
import com.lizonghui.googleplay.base.BaseHolder;
import com.lizonghui.googleplay.bean.DownLoadInfo;
import com.lizonghui.googleplay.bean.ItemInfoBean;
import com.lizonghui.googleplay.manager.DownLoadManger;
import com.lizonghui.googleplay.utils.CommonUtils;
import com.lizonghui.googleplay.utils.LogUtils;
import com.lizonghui.googleplay.utils.UIUtils;
import com.lizonghui.googleplay.views.ProgressButton;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/6/25.
 * 模块化视图设计
 * 数据类型就仍是DetailInfoBean
 * <p/>
 * 观察者-->接收消息的人
 */
public class DetailDownHolder extends BaseHolder<ItemInfoBean> implements DownLoadManger.DownLoadInfoObserver {
    @Bind(R.id.app_detail_download_btn_favo)
    Button mAppDetailDownloadBtnFavo;
    @Bind(R.id.app_detail_download_btn_share)
    Button mAppDetailDownloadBtnShare;
    @Bind(R.id.app_detail_download_btn_download)
    ProgressButton mAppDetailDownloadBtnDownload;
    private ItemInfoBean mBean;


    //绑定数据，刷新UI
    @Override
    public void refreshHolderView(ItemInfoBean data) {
        mBean = data;

        /*-------------2-根据不同状态，显示不同UI----------------------*/
        //首先，通过detailInfoBean来得到downLoadInfo
        DownLoadInfo downLoadInfo = DownLoadManger.getInstance().getDownLoadInfo(mBean);
        refreshProgressButton(downLoadInfo);

    }

    //根据不同状态刷新UI
    private void refreshProgressButton(DownLoadInfo downLoadInfo) {
        int currentState = downLoadInfo.currentState;
        switch (currentState) {
            case DownLoadManger.STATE_UNDOWNLOAD:
                mAppDetailDownloadBtnDownload.setText("下载");
                break;
            case DownLoadManger.STATE_DOWNLOADING:
                mAppDetailDownloadBtnDownload.setBackgroundResource(R.drawable.selector_app_detail_bottom_downloading);
                int progress = (int) (downLoadInfo.progress * 1.0f / downLoadInfo.size * 100 + .5f);
                mAppDetailDownloadBtnDownload.setText("下载中.." + progress + "%");
                mAppDetailDownloadBtnDownload.setMax(downLoadInfo.size);
                mAppDetailDownloadBtnDownload.setProgress(downLoadInfo.progress);
                break;
            case DownLoadManger.STATE_PAUSEDOWNLOAD:
                mAppDetailDownloadBtnDownload.setText("暂停下载，点击继续");
                break;
            case DownLoadManger.STATE_WAITINGDOWNLOAD:
                mAppDetailDownloadBtnDownload.setText("等待下载，点击取消");
                break;
            case DownLoadManger.STATE_DOWNLOADFAILED:
                mAppDetailDownloadBtnDownload.setText("下载失败，点击重试");
                break;
            case DownLoadManger.STATE_DOWNLOADED:
                mAppDetailDownloadBtnDownload.setBackgroundResource(R.drawable.selector_app_detail_bottom_normal);
                mAppDetailDownloadBtnDownload.setText("下载完成，点击安装");
                break;
            case DownLoadManger.STATE_INSTALLED:
                mAppDetailDownloadBtnDownload.setText("已安装，点击打开");
                break;
        }
    }

    //初始化视图,找到孩子
    @Override
    public View initHolderView() {
        View rootView = View.inflate(UIUtils.getContext(), R.layout.item_detail_down, null);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick(R.id.app_detail_download_btn_download)
    public void onClick() {
        /*-------------3根据不同状态，响应不同操作----------------------*/
        DownLoadInfo downLoadInfo = DownLoadManger.getInstance().getDownLoadInfo(mBean);
        LogUtils.d("downLoadTask", "downLoadInfo" + downLoadInfo.toString());
        int currentState = downLoadInfo.currentState;
        switch (currentState) {
            case DownLoadManger.STATE_UNDOWNLOAD:
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManger.STATE_DOWNLOADING:
                pauseDownLoad(downLoadInfo);
                break;
            case DownLoadManger.STATE_PAUSEDOWNLOAD:
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManger.STATE_WAITINGDOWNLOAD:
                cancelDownLoad(downLoadInfo);
                break;
            case DownLoadManger.STATE_DOWNLOADFAILED:
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManger.STATE_DOWNLOADED:
                installApk(downLoadInfo);
                break;
            case DownLoadManger.STATE_INSTALLED:
                openApk(downLoadInfo);
                break;
        }
    }

    private void openApk(DownLoadInfo downLoadInfo) {
        CommonUtils.openApp(UIUtils.getContext(), downLoadInfo.packageName);
    }

    private void installApk(DownLoadInfo downLoadInfo) {
        File saveApk = new File(downLoadInfo.savePath);
        if (saveApk == null) {
            Toast.makeText(UIUtils.getContext(), "文件不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        CommonUtils.installApp(UIUtils.getContext(), saveApk);
    }

    private void cancelDownLoad(DownLoadInfo downLoadInfo) {
        DownLoadManger.getInstance().cancelDownLoad(downLoadInfo);
    }

    private void pauseDownLoad(DownLoadInfo downLoadInfo) {
        //暂停我们交给DownLoadManager去处理,改变downLoadInfo状态
        DownLoadManger.getInstance().pauseDownLoad(downLoadInfo);
    }

    private void doDownLoad(DownLoadInfo downLoadInfo) {
        DownLoadManger.getInstance().doDownLoad(downLoadInfo);
    }

    /**
     * 接到信息改变的通知
     * @param downLoadInfo
     */
    @Override
    public void onDownLoadInfoChanged(final DownLoadInfo downLoadInfo) {
        //被观察者发布消息，每个人都值需影响自己有关的消息即可。要过滤传递过来的消息
        if (!downLoadInfo.packageName.equals(mBean.packageName)) {
            return;
        }

        //刷新UI
        UIUtils.postTaskSafely(new Runnable() {
            @Override
            public void run() {
                refreshProgressButton(downLoadInfo);
            }
        });
    }
}
